#include "include.h"
 
/*
		函数功能：点阵数据顺时针旋转90度
		*in				点阵数据
*/
void C2R(char *in)
{
 unsigned char r[8];
 unsigned char a,b,c;

	for(a = 0;a < 8;a ++)
		r[a] = 0;

	for(a = 0;a < 8;a ++)
	{
		for(b = 0;b < 8;b ++)
		{
			r[a] >>= 1;
		
			if((in[b] & (0x01 << a)) != 0)
				r[a] |= 0x80;
		}
	}

	for(a = 0;a < 8;a ++)
		in[a] = r[a];	
}

/*
		函数功能：调整字库芯片提取的数据8*8块排列
		*p2			字库芯片点阵数据
		*p1			转换后点阵数据输出
*/
void ChangeLedBlock(char *p1,char *p2)
{
	p1[0] = p2[24];
	p1[1] = p2[25];
	p1[2] = p2[26];
	p1[3] = p2[27];
	p1[4] = p2[28];
	p1[5] = p2[29];
	p1[6] = p2[30];
	p1[7] = p2[31];
	
	p1[8] = p2[8];
	p1[9] = p2[9];
	p1[10] = p2[10];
	p1[11] = p2[11];
	p1[12] = p2[12];
	p1[13] = p2[13];
	p1[14] = p2[14];
	p1[15] = p2[15];	
	
	p1[16] = p2[16];
	p1[17] = p2[17];
	p1[18] = p2[18];
	p1[19] = p2[19];
	p1[20] = p2[20];
	p1[21] = p2[21];
	p1[22] = p2[22];
	p1[23] = p2[23];	
	
	p1[24] = p2[0];
	p1[25] = p2[1];
	p1[26] = p2[2];
	p1[27] = p2[3];
	p1[28] = p2[4];
	p1[29] = p2[5];
	p1[30] = p2[6];
	p1[31] = p2[7];		
}


/*
		函数功能：调整字库芯片提取的数据8*8块排列
		*p2			字库芯片点阵数据
		*p1			转换后点阵数据输出
*/
void ChangeLedBlock_ascii(char *p1,char *p2)
{
	p1[0] = p2[8];
	p1[1] = p2[9];
	p1[2] = p2[10];
	p1[3] = p2[11];
	p1[4] = p2[12];
	p1[5] = p2[13];
	p1[6] = p2[14];
	p1[7] = p2[15];
	
	p1[8] = p2[0];
	p1[9] = p2[1];
	p1[10] = p2[2];
	p1[11] = p2[3];
	p1[12] = p2[4];
	p1[13] = p2[5];
	p1[14] = p2[6];
	p1[15] = p2[7];	
}
/*
		函数功能：调整字库芯片提取的数据8*8块排列
		*p2			字库芯片点阵数据
		*p1			转换后点阵数据输出
*/
void ChangeLedBlock_2(char *p1,char *p2)
{
	p1[0] = p2[0];
	p1[1] = p2[1];
	p1[2] = p2[2];
	p1[3] = p2[3];
	p1[4] = p2[4];
	p1[5] = p2[5];
	p1[6] = p2[6];
	p1[7] = p2[7];
	
	p1[8] = p2[8];
	p1[9] = p2[9];
	p1[10] = p2[10];
	p1[11] = p2[11];
	p1[12] = p2[12];
	p1[13] = p2[13];
	p1[14] = p2[14];
	p1[15] = p2[15];	
}
  
/*
		函数功能：显示ascii码	16汉字
		*word		汉字字符串
*/

	char led_buf1[32][16];
	char led_buf2[32][16];

void led_display_text(char *word2,unsigned short speed)
{
	unsigned short i,j,c;							
	
	unsigned char word_pos = 0;
	unsigned char str_len = 0;
	
	char word_buf_1[MAX_DISPLAY_ASCII];
	char word_buf_2[MAX_DISPLAY_ASCII];	
	
	char word1[MAX_DISPLAY_ASCII];
	char word3[MAX_DISPLAY_ASCII];

	str_len = strlen(word2);//获取要显示的长度
 
	if(str_len > MAX_DISPLAY_ASCII - ROLL_INS_SPACE)//超16汉字不显示 要考虑字符串前插入的空格
		return;
	
	if(str_len <= MAX_FULL_DISPLAY_ASCII)//小于全屏长度
	{
		sprintf(word3,"%s",word2);				
		
		for(j = 0;j < (MAX_FULL_DISPLAY_ASCII - str_len);j ++)//末尾添加空格 直到 全屏显示
			sprintf(word3 + strlen(word3)," ");		
	}
	else
	{
		for(j = 0;j < ROLL_INS_SPACE;j ++)//插入4个空格
			word3[j] = ' ';		
		
		for(j = 0;j < str_len + 1;j ++)//复制到word3
			word3[j + ROLL_INS_SPACE] = word2[j];
	}
	
	str_len = strlen(word3);//获取调整后字符串长度
																													
	for(j = 0;j < MAX_DISPLAY_ASCII;j ++)
	  word1[j] = 0;
	
	for(j = 0;j < str_len;j ++)//反转字符串内容
	{
		if(*(word3 + j) < 0x80)//ascii
		{
		 		word1[(str_len - 1) - j] = word3[j];
		}
		else//gb2312
		{
				word1[(str_len - 2) - j] = word3[j + 0];
				word1[(str_len - 1) - j] = word3[j + 1];
				j ++;
		}
	}
	
	GT_Init();
	
	while(*(word1 + word_pos) != 0)//提取字符的模
	{
		if(*(word1 + word_pos) < 0x80)//ascii
		{
			ASCII_GetData(*(word1 + word_pos),ASCII_8X16_F,word_buf_1);
			ChangeLedBlock_ascii(&led_buf1[word_pos][0],word_buf_1);				
			word_pos += 1;			
		}
		else//gb2312
		{
			gt_16_GetData(*(word1 + word_pos + 0),*(word1 + word_pos + 1),word_buf_1);
			ChangeLedBlock(word_buf_2,word_buf_1);			
			ChangeLedBlock_2(&led_buf1[word_pos + 0][0],&word_buf_2[0]);			
			ChangeLedBlock_2(&led_buf1[word_pos + 1][0],&word_buf_2[16]);						
			word_pos += 2;			
		}
	}	
	
	for(j = 0;j < str_len;j ++)//调整扫描方向
	{
		for(i = 0;i < 2;i ++)	
				C2R(&led_buf1[j][i * 8]);		
	}

	KeepSeting_MAX7219();//刷新设置
  
	for(i = 0;i < 8;i++)//显示
	{
		MAX7219_PIN_CS_0
		for(j = 0;j < (str_len * 2);j ++)
		{
			Write_Max7219_byte(i + 1);   
			Write_Max7219_byte(led_buf1[(i + (j * 8)) / 16][(i + (j * 8)) % 16]); 			
		}
		MAX7219_PIN_CS_1																								 
	}		
 
	if(str_len <= MAX_FULL_DISPLAY_ASCII)//不超屏幕 退出滚动
		return;
	
	for(c = 0;c < str_len * 8;c++)
	{
		Delay_ms(speed);//调速

		for(i = 0;i < str_len * 16;i ++)//移动数据
		{
				if(led_buf1[0][(str_len * 16 - 1) - i] & 0x01)
				{				
					led_buf1[0][(str_len * 16 - 1) - i + 16] |= 0x80;
				}
				else
				{
					led_buf1[0][(str_len * 16 - 1) - i + 16] &= ~0x80;				
				}					
				
			led_buf1[0][(str_len * 16 - 1) - i] = led_buf1[0][(str_len * 16 - 1) - i] >> 1;		
		}			

		for(i = 0;i < 8;i++)//显示
		{
			MAX7219_PIN_CS_0
			for(j = 0;j < (str_len * 2);j ++)
			{
				Write_Max7219_byte(i + 1);   
				Write_Max7219_byte(led_buf1[(i + (j * 8)) / 16][(i + (j * 8)) % 16]); 						
			}																
			MAX7219_PIN_CS_1									
		}																														
	}											
}





